import pymongo

# 连接到MongoDB
client = pymongo.MongoClient("mongodb://localhost:27017/")

# 选择数据库
db = client["mydatabase"]

# 选择集合
collection = db["mycollection"]

# 插入文档
data = {"name": "John", "age": 30}
result = collection.insert_one(data)
print("插入的文档ID:", result.inserted_id)

# 查询所有文档
print("所有文档:")
for document in collection.find():
    print(document)

# 查询年龄大于25的文档
query = {"age": {"$gt": 25}}
results = collection.find(query)
print("年龄大于25的文档:")
for document in results:
    print(document)

# 更新年龄小于30的文档
query = {"age": {"$lt": 30}}
new_values = {"$set": {"age": 35}}
collection.update_many(query, new_values)

# 删除年龄大于40的文档
query = {"age": {"$gt": 40}}
collection.delete_many(query)